
/*
 * \class function formValidationHandler
 * \brief This class is used to valide the form content on the client side.
 * \author Salman Noor
 */


var formValidator = new formValidationHandler();
function formValidationHandler()
{
    this.checkLength = function(o,n,min,max) {

        if ( o.val().length > max || o.val().length < min ) {
            o.addClass('ui-state-error');
            this.updateErrorMessage("Length of " + n + " must be between "+min+" and "+max+".");
            return false;
        } else {
            return true;
        }

    }

    this.checkIfEmpty = function (o)
    {
        if ( o.val().length <= 0 ) {
            o.addClass('ui-state-error');
            this.updateErrorMessage("Field is Empty.");
            return false;
        } else {
            return true;
        }
    }

    this.checkPasswordFields = function(fld1,fld2)
    {
        if ( fld1.val() ==  fld2.val())
        {
            return true;
        }
        else
        {
            fld1.addClass('ui-state-error');
            fld2.addClass('ui-state-error');
            this.updateErrorMessage("Password fields do not match.");

        }
    }

    this.updateErrorMessage = function(errorText) {

        jQuery('.errorMessageDiv').css("color","red");
        jQuery('.errorMessageDiv').html("Error. ");

        jQuery('.errorMessageDiv').append(errorText)
        .addClass('ui-state-highlight');
        jQuery('.errorMessageDiv').show('slow');
        setTimeout(function() {
            jQuery('.errorMessageDiv').removeClass('ui-state-highlight', 1500);
        }, 1500);
    }

    this.checkRegexp = function(o,regexp,n) {

        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass('ui-state-error');
            this.updateErrorMessage(n);
            return false;
        } else {
            return true;
        }

    }
}